import logging
from typing import Text

import allure
import pymysql
import requests

from config.config import *


class RequestControl:
    def __init__(self, in_data):
        self.request_data = in_data

    # @staticmethod
    # def api_allure_step(
    #         *,
    #         url: Text,
    #         method: Text,
    #         headers: Text,
    #         data: Text,
    #         assert_data: Text,
    #         res: Text,
    # ):
    #     pass

    # 用于具体值
    # def allure_step

    # 用于展示字典值
    # def allure_step_no

    @allure.step('发起请求')
    def http_request(self):
        _data = {
            'url': self.request_data['url'],
            'method': self.request_data['method'],
            'headers': self.request_data['headers'],
            'params': self.request_data['params'],
            'data': self.request_data['data'],
            'json': self.request_data['json'],
            'files': self.request_data['files'],
        }
        logging.info(f"\n====请求路径:      {_data['url']}\n" 
                     f"====请求方式:      {_data['method']}\n" 
                     f"====请求头:        {_data['headers']}\n" 
                     f"====请求内容:       data:{_data['data']}    json:{_data['json']}    files:{_data['files']}\n"
                     )
        return requests.request(**_data)


    def jdbc_request(self, sql, index=0):
        conn = pymysql.connect(
            host=DB_HOST,
            port=DB_PORT,
            user=DB_USERNAME,
            password=DB_PASSWORD,
            database=DB_NAME,
            charset='utf8'
        )
        cursor = conn.cursor()
        cursor.execute(sql)
        res = cursor.fetchone()

        cursor.close()
        conn.close()

        return res[index]
